package com.xjrsoft.module.customerTwo.ycode.utils;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.xjrsoft.core.tool.utils.DateUtil;
import com.xjrsoft.core.tool.utils.IoUtil;
import com.xjrsoft.core.tool.utils.StringPool;
import com.xjrsoft.module.base.entity.XjrBaseAnnexesFile;
import lombok.extern.slf4j.Slf4j;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.Base64;
import java.util.Date;

/**
 * 椅业云码工具类
 */
@Slf4j
public class YCodeUtils {
    private static final int QR_CODE_SIZE = 200;
    private static final String fileDirPath = StringPool.SLASH + "static" + StringPool.SLASH + "PRINT_IMG" + StringPool.SLASH + DateUtil.format(new Date(), "yyyyMM");


    /**
     * 根据文字生成二维码图片
     * @param text 需要生成二维码的内容
     * @return 生成图片的相对路径
     */
    public static String createQRCode(String text){
        String rootPath = IoUtil.getProjectPath();
        rootPath = rootPath.substring(1,rootPath.length()-1);
        String monthFormat = DateUtil.format(new Date(), "yyyyMM");
        // 保存文件到文件夹
        String fileDirPath =  StringPool.SLASH+"static" + StringPool.SLASH + "QRCode_IMG" + StringPool.SLASH + monthFormat;
        String filePath = fileDirPath+ StringPool.SLASH +IdWorker.get32UUID()+".png";
        File fileDir = new File(rootPath + fileDirPath);
        if (!fileDir.exists()){
            fileDir.mkdirs();
        }
        try{
            QRCodeWriter qrCodeWriter = new QRCodeWriter();
            BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, QR_CODE_SIZE, QR_CODE_SIZE);
            Path file = FileSystems.getDefault().getPath(rootPath+filePath);
            MatrixToImageWriter.writeToPath(bitMatrix, "PNG", file);
            return filePath;
        }catch (Exception e){
            log.error("YCodeUtils createQRCode error",e);
            return null;
        }
    }

    /**
     * base64转图片
     * @param base64Str
     * @param fileName
     */
    public static XjrBaseAnnexesFile convertBase64ToImage(String base64Str, String fileName) {
        if(base64Str.indexOf("")>-1){
            base64Str = base64Str.replace("","");
        }
        byte[] imageBytes = Base64.getDecoder().decode(base64Str);
        try {
            XjrBaseAnnexesFile annexesFile = new XjrBaseAnnexesFile();

            ByteArrayInputStream bis = new ByteArrayInputStream(imageBytes);
            BufferedImage image = ImageIO.read(bis);
            bis.close();
            File fileDir = new File(IoUtil.getProjectPath() + fileDirPath);
            if (!fileDir.exists()){
                fileDir.mkdirs();
            }
            File outputFile = new File(IoUtil.getProjectPath() +fileDirPath+ StringPool.SLASH+fileName);
            ImageIO.write(image, "png", outputFile);
            annexesFile.setFilePath(fileDirPath+ StringPool.SLASH+fileName);
            annexesFile.setFileName(fileName);
            annexesFile.setFileExtensions(StringPool.DOT +"png");
            annexesFile.setFileType("png");
            annexesFile.setFileSize(outputFile.length()+"");
            return annexesFile;
        } catch (IOException e) {
            log.error("YCodeUtils convertBase64ToImage error========================>",e);
        }
        return null;
    }
    public static void main(String[] args){
        System.out.println(createQRCode("恭喜发财"));
    }

}
